<?php
/**
 * Created by PhpStorm.
 * User: wugang
 * Date: 2017/11/25
 * Time: 下午5:34
 */

namespace App\Models;


use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Task
 *
 * @mixin \Eloquent
 * @property int $id
 * @property string $desc 任务简介
 * @property int $uid 用户id
 * @property int $amount 监督奖励金额
 * @property int $repeat_times 习惯养成期间任务需重复完成次数
 * @property int $start_time 任务开始时间
 * @property int $end_time 任务结束时间
 * @property int $state 任务状态
 * 0：已发布
 * 1：已完成
 * 2：未完成
 * @property string $supervisors 监督用户的uid，逗号分隔
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Task whereAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Task whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Task whereDesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Task whereEndTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Task whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Task whereRepeatTimes($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Task whereStartTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Task whereState($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Task whereSupervisors($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Task whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Task whereUpdatedAt($value)
 * @property-read mixed $task_join_users
 */
class Task extends Model
{

    protected $table = 'hackathon.task';


    public function getTaskJoinUsersAttribute(){
        $supervisor_users = $this->supervisors;
        if($supervisor_users){
            $ids = explode(',',$supervisor_users);
            return User::whereIn('id',$ids)->get(['id','name','head_img_url']);
        }
        return (new Collection([]));
    }

    public function createUser(){
        return $this->belongsTo(User::class, 'uid');
    }
}